package odgs.app;

import java.awt.Frame;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import odgs.MoreSizeDistributedSelection;
import odgs.MoreSizeSelection;
import odgs.SelectionStrategy;
import odgs.app.server.ServerMainFrameController;
import odgs.app.server.ServerMainFrameUI;
import odgs.manager.DBManager;
import odgs.manager.MySqlDBManager;
import odgs.manager.NetworkManager;
import odgs.manager.ServerManager;
import odgs.manager.filedb.FileDBManager;
import odgs.message.ServerMessageProcessor;

public class GridServer
{
	
	private NetworkManager networkManager;
	private ServerManager serverManager;
	private DBManager dbManager;
	private ServerMessageProcessor serverProcessor;
	
	//Properties
	private int port = 7777;
	public int portPDA = 7778;
	
	private long distrbutedLimitSize = 500000000L;
	private String distrbutedPolicy = "NoDistribute";
	
	private String dbType = "FileDB";
	private String fileDbPath = "./file_db";
	private String dbURL;
	private String dbUser;
	private String dbPasswd;
	private String dbDirver; 
	
	
	public GridServer()
	{
		networkManager = new NetworkManager();
		serverManager = new ServerManager();

		System.out.println("GridServer.GridServer()");
		loadProperties();
		if(dbType.equals("FileDB"))
			dbManager = new FileDBManager(fileDbPath,true, true);
		else if(dbType.equals("MySQL"))
			dbManager = new MySqlDBManager(dbDirver, dbURL, dbUser, dbPasswd);
		else
			dbManager = new FileDBManager(fileDbPath,true, true);
			
		
		serverManager.setDBManager(dbManager);
		serverManager.setNetworkManager(networkManager);
		
		SelectionStrategy strategy = null;
		if(distrbutedPolicy.equals("NoDistribute"))
			strategy = new MoreSizeSelection();
		else if(distrbutedPolicy.equals("MoreSizeDistributedSelection"))
			strategy = new MoreSizeDistributedSelection(distrbutedLimitSize);
			
		serverManager.setNodeSelectionStrategy(strategy);
		serverProcessor = new ServerMessageProcessor();
		serverProcessor.setNetworkManager(networkManager);
		serverProcessor.setServerManager(serverManager);

		ServerMainFrameUI ui = new ServerMainFrameUI();
		
		ServerMainFrameController controller = new ServerMainFrameController(ui);
		controller.setServerManager(serverManager);
		controller.setDBManager(dbManager);
		controller.setNetworkManager(networkManager);

		serverManager.openToServer(port, portPDA);
	}

	private void loadProperties() {
		try {
			Properties env = new Properties();
			
			env.load(new FileInputStream("server.properties"));
			port = Integer.parseInt(((String)env.get("PORT")));
			distrbutedPolicy = (String)env.get("DISTRIBUTED_POLICY");
			distrbutedLimitSize = Long.parseLong((String)env.get("DISTRIBUTED_LIMIT_SIZE"));

			String pda = (String)env.get("PDA_SUPPORT");
			if(pda.equals("OK"))
			{
				portPDA = Integer.parseInt(((String)env.get("PORT_PDA")));
			}

			dbType = (String)env.get("DB_TYPE");
			if(dbType.equals("FileDB"))
			{
				fileDbPath = (String)env.get("FILE_DB_PATH");
			}
			else
			{
				dbDirver = (String)env.get("DB_DIRVER");
				dbURL = (String)env.get("DB_URL");
				dbUser = (String)env.get("DB_USER");
				dbPasswd = (String)env.get("DB_PASSWD");
			}

		} catch (Exception e) {
			e.printStackTrace();
			SwingUtil.showCiritalErrorDialog(new JFrame(),"invalid setting value \"server.properties\"");
		}
	}
}